# 登录需要
import hashlib
import json

# 接口测试需要
import requests

# -----------------------------------------   登录 -------------------------------------------------------
# 构造请求头
headers = {

    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
    'Content-Type': 'application/json;charset=utf-8'
}
# 'token': ''

# 构造url
base_url = r'https://cloud.huaxiaerp.com/'
url_login = base_url + '/jshERP-boot/user/login'

# 构造data
username = 'Missing'
password = 'h123456'
md5 = hashlib.md5(password.encode())  # 创建MD5对象
password_md5 = md5.hexdigest()  # 调用方法hexdigest加密
data_login = {
    "loginName": username,
    "password": password_md5
}
# 发送情求
resp = requests.post(url_login, json=data_login, headers=headers, verify=False)

# 获取响应
token = resp.json()['data']['token']
print(token)

# ----------------------------------------零售出库：新增----------------------------------------------------
# 构造操作headers_追加token
headers['X-Access-Token'] = token

# 构造url
base_url = r'https://cloud.huaxiaerp.com'
url_retail_add = base_url + r'/jshERP-boot/depotHead/addDepotHeadAndDetail'
# 构造数据
# ---- 动态数据 ----
number = 'LSCK00000726390'
accountId = 11902  # 账户名
id = 17050534866660885801
defaultNumber = 'LSCK00000726390'
null = ''
# data_retail_add = {
#     "info": "{\"organId\":142861,\"operTime\":\"2024-01-14 01:38:44\",\"number\":\"LSCK00000727282\",\"payType\":\"现付\",\"changeAmount\":\"500.00\",\"getAmount\":\"500.00\",\"backAmount\":0,\"accountId\":11902,\"type\":\"出库\",\"subType\":\"零售\",\"defaultNumber\":\"LSCK00000727282\",\"totalPrice\":500,\"fileName\":\"\",\"status\":\"0\"}",
#     "rows": "[{\"id\":\"17051675241790659474\",\"name\":\"眼影\",\"standard\":null,\"model\":null,\"color\":null,\"materialOther\":\"\",\"stock\":90,\"unit\":\"盒\",\"snList\":\"\",\"batchNumber\":\"\",\"expirationDate\":\"\",\"sku\":\"\",\"operNumber\":\"5\",\"unitPrice\":100,\"allPrice\":500,\"remark\":\"\",\"orderNum\":1,\"depotId\":\"19957\",\"barCode\":\"1001\"}]"
# }
info_dict = {"organId ": 142861, "operTime ": "2024-01-14 01:38:44 ", "number ": "LSCK00000727282 ",
             "payType ": "现付 ", "changeAmount ": "500.00 ", "getAmount ": "500.00 ", "backAmount ": 0,
             "accountId ": 11902, "type ": "出库 ", "subType ": "零售 ", "defaultNumber ": "LSCK00000727282 ",
             "totalPrice ": 500, "fileName ": " ", "status ": "0 "}
info_str = json.dumps(info_dict)

rows_dict = [{"id ": id, "name ": "眼影 ", "standard ": null, "model ": null, "color ": null, "materialOther ": " ",
              "stock ": 90, "unit ": "盒 ", "snList ": " ", "batchNumber ": " ", "expirationDate ": " ", "sku ": " ",
              "operNumber ": "5 ", "unitPrice ": 100, "allPrice ": 500, "remark ": " ", "orderNum ": 1,
              "depotId ": "19957 ", "barCode ": "1001 "}]
rows_str = json.dumps(rows_dict)

data_retail_add = {
    'info': info_str,
    'rows': rows_str
}

# 发送请求
resp = requests.post(url_retail_add, headers=headers, json=data_retail_add, verify=False)
# 获取响应
print(resp.text)

# ----------------------------------------零售出库：修改----------------------------------------------------
# 构造 headers
headers['X-Access-Token'] = token
# 构造url
url_retail_mod = base_url + r'/jshERP-boot/depotHead/updateDepotHeadAndDetail'
id = 387350
number = 'LSCK00000726389'
# 构造数据
info_dict = {"id ": id, "type ": "出库 ", "subType ": "零售 ", "defaultNumber ": number,
             "number ": number, "createTime ": "2024-01-12T09:58:27.000+0000 ",
             "operTime ": "2024-01-12 17:58:06 ", "organId ": null, "creator ": 151170, "accountId ": 11902,
             "changeAmount ": 1000, "backAmount ": -300, "totalPrice ": 1000, "payType ": "现付 ", "billType ": null,
             "remark ": null, "fileName ": " ", "salesMan ": null, "accountIdList ": null, "accountMoneyList ": null,
             "discount ": null, "discountMoney ": null, "discountLastMoney ": 0, "otherMoney ": null, "deposit ": 0,
             "status ": "0 ", "purchaseStatus ": "0 ", "source ": "0 ", "linkNumber ": null, "tenantId ": 151170,
             "deleteFlag ": "0 ", "projectName ": null, "organName ": null, "userName ": null,
             "accountName ": "公司花西子收支账户 ", "allocationProjectName ": null, "materialsList ": "眼影    ",
             "salesManStr ": null, "operTimeStr ": "2024-01-12 17:58:06 ", "finishDebt ": null,
             "depotHeadType ": "零售出库 ", "creatorName ": "Missing ", "contacts ": null, "telephone ": null,
             "address ": null, "finishDeposit ": null, "needDebt ": null, "debt ": -1000, "materialCount ": null,
             "hasFinancialFlag ": False, "hasBackFlag ": False, "realNeedDebt ": null, "getAmount ": "700.00 "}
info_str = json.dumps(info_dict)
# 构造数据2
rows_dict = [{"id ": id, "name ": "眼影 ", "standard ": null, "model ": null, "color ": null, "materialOther ": " ",
              "stock ": 90, "unit ": "盒 ", "snList ": " ", "batchNumber ": " ", "expirationDate ": " ", "sku ": " ",
              "operNumber ": "5 ", "unitPrice ": 100, "allPrice ": 500, "remark ": " ", "orderNum ": 1,
              "depotId ": "19957 ", "barCode ": "1001 "}]
rows_str = json.dumps(rows_dict)

data_retail_mod = {
    'info': info_str,
    'rows': rows_str
}

# 发送请求
resp = requests.put(url_retail_mod, headers=headers, json=data_retail_mod, verify=False)
# 获取响应
print(resp.text)

# ----------------------------------------零售出库：查询------------------------------------
# 构造url
url_retail_query = base_url + r'/jshERP-boot/depotHead/list'
# 构造headers
headers['X-Access-Token'] = token
# 构造数据
materialParam = '眼影',
# 内层数据设置为字典
search_dict = {"number": "", "materialParam": materialParam, "type": "出库", "subType": "零售", "organId": "", "depotId": "",
               "creator": "", "accountId": "", "status": "", "remark": ""}
# 将字典类型转换为字符串
search_str = json.dumps(search_dict)
data_retail_query = {
    'search': search_str,
    'column': 'createTime',
    'order': 'desc',
    'field': 'id,'', action, organName,number,materialsList,operTimeStr,userName,materialCount,totalPrice,getAmount,backAmount,status',
    'currentPage': 1,
    'pageSize': 10
}
# 发送请求
resp = requests.get(url_retail_query, params=data_retail_query, headers=headers, verify=False)
# 获取响应
print(resp.text)

# ------------------------------------零售出库： 删除--------------------------------------------------
# 构造 url
url_retail_delete = base_url + r'/jshERP-boot/depotHead/delete'
# 构造 headers
headers['X-Access-Token'] = token
# 构造数据
id = 388009
data_retail_delete = {
    'id': id
}
# 发送请求
resp = requests.delete(url_retail_delete,params=data_retail_delete,headers=headers,verify=False)
# 获取响应
print(resp.text)
# ------------------------------------- 零售出库：订单审核 --------------------------------------------
# 构造 url
url_retail_checkout = base_url + r'/jshERP-boot/depotHead/batchSetStatus'
# 构造 headers
headers['X-Access-Token'] = token
# 构造数据
status = 1
id = 387350
data_retail_check = {
    "status":status,
    "ids": id
}
# 发送请求
resp = requests.post(url_retail_checkout,json= data_retail_check,headers=headers,verify=False)
# 获取响应
print(resp.text)

